package com.jake.spring.mvc.controller;

import com.jake.spring.mvc.config.ValueConfig;
import com.jake.spring.mvc.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("persons")
public class PersonController {

    @Autowired
    private ValueConfig valueConfig;

    @GetMapping("")
    public ModelAndView getPersons() {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Jake", valueConfig.getAge()));
        persons.add(new Person("Heather", valueConfig.getAge()));
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("persons", persons);
        modelAndView.setViewName("person");
        return modelAndView;
    }

    @GetMapping("string")
    public String getPersons(Model model) {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Jake", 27));
        persons.add(new Person("Heather", 27));
        model.addAttribute("persons", persons);
        return "person";
    }

    @GetMapping("json")
    @ResponseBody
    public List<Person> getPersonsJson() {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Jake", 27));
        persons.add(new Person("Heather", 27));
        return persons;
    }
}
